package com.platform.server.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.paltform.server.entity.ExcelOrder;
import com.paltform.server.entity.ExcelUserInfo;
import com.platform.server.dao.OrderDao;
import com.platform.server.response.pojo.ReturnData;
import com.platform.server.service.OrderService;
import com.platform.server.util.ExportExcel;
import com.platform.server.util.StringTools;

import jersey.repackaged.com.google.common.collect.Lists;

@Controller
public class OrderController {

	@Autowired
	private OrderService orderService;

	@Autowired
	private OrderDao orderDao;

	/**
	 * 订单列表
	 */
	@RequestMapping("/selectOrderList")
	public Object selectOrderList(String id, String startTime, String endTime, String status, String order_id,
			String mobile, String serveraddres, String serverName, String type, String enter,String subtype,
			HttpServletRequest request) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> lists = orderService.selectOrderList(id, startTime, endTime, status, order_id,
				serveraddres, mobile, type,subtype);
		for (int i = 0; i < lists.size(); i++) {
			Map<String, Object> map = new HashMap<>();
			lists.get(i).put("create_time", sdf.format(lists.get(i).get("create_time")));
			lists.get(i).put("server_time", sdf.format(lists.get(i).get("server_time")));
			if (lists.get(i).get("create_times") != null) {
				lists.get(i).put("create_times", sdf.format(lists.get(i).get("create_times")));
			}
			if ("1".equals(lists.get(i).get("order_type"))) {
				map = orderDao.selectServerType(Integer.parseInt(lists.get(i).get("server_id").toString()));
			} else if ("2".equals(lists.get(i).get("order_type"))) {
				map = orderDao.selectDemandType(Integer.parseInt(lists.get(i).get("server_id").toString()));
			}
			if (map == null) {
				lists.remove(i);
				i--;
				continue;
			}
			/*if(!StringTools.isEmpty(serverName) && !StringTools.isEmpty(subtype)){
				String server_type = map.get("server_type").toString();
				String server_sub_type = map.get("server_sub_type").toString();
				if(!server_type.equals(serverName)&& !server_sub_type.equals(subtype)){
					lists.remove(i);
					i--;
					continue;
				}
			}else if(!StringTools.isEmpty(serverName)){
				if(!map.get("server_type").equals(serverName)){
					lists.remove(i);
					i--;
					continue;
				}
			}*/
			//System.out.println(lists.get(i).get("server_id") + ":" + lists.get(i).get("order_type"));
			lists.get(i).put("server_type", map.get("server_type"));
			lists.get(i).put("server_sub_type", map.get("server_sub_type"));
			lists.get(i).put("details", map.get("details"));
			lists.get(i).put("address", map.get("address"));
		}
		  List<Map<String,Object>> list2 =Lists.newArrayList();
		if(!StringTools.isEmpty(serverName) && !StringTools.isEmpty(subtype)){
			list2 = lists.stream().filter((Map a) -> (serverName.equals(a.get("server_type").toString())&& subtype.equals(a.get("server_sub_type")))).collect(Collectors.toList());	
		 }else{		
			 if(!StringTools.isEmpty(serverName)){
				 list2 = lists.stream().filter((Map a) -> (serverName.equals(a.get("server_type").toString()))).collect(Collectors.toList());	
			 }	
		 }
		
		
		if(!StringTools.isEmpty(serverName)){
			request.setAttribute("lists", list2);
			request.setAttribute("nums", list2.size());
		}else{
			request.setAttribute("lists", lists);
			request.setAttribute("nums", lists.size());
		}
		
		if (!StringTools.isEmpty(id)) {
			if (!StringTools.isEmpty(enter)) {
				request.setAttribute("enter", enter);
			}
			return "/ui/order/order_info.jsp";
		}
		return "/ui/order/order_list.jsp";
	}

	/**
	 * 删除订单
	 */
	@RequestMapping("/deleteOrder")
	public Object deleteOrder(String idd) {
		int flag = orderDao.deleteOrder(idd);
		return "/selectOrderList";
	}

	// 导出报表
	@RequestMapping("/orderExportExcel")
	public void orderExportExcel(String id, String startTime, String endTime, String status, String order_id,
			String serveraddres, String serverName, String mobile, String type, String subtype,HttpServletResponse response) {
		List<Object> list2 = new ArrayList<Object>();
		Map<String, Object> map = new HashMap<>();
		List<Map<String, Object>> lists = orderService.selectOrderList(id, startTime, endTime, status, order_id,
				serveraddres, mobile, type,subtype);
		for (int i = 0; i < lists.size(); i++) {
			ExcelOrder order = new ExcelOrder();
			order.setOrderId(lists.get(i).get("order_id"));
			order.setByMobile(lists.get(i).get("by_mobile"));
			order.setByUsername(lists.get(i).get("by_username"));
			order.setCreateTime(lists.get(i).get("create_time"));
			if (lists.get(i).get("enterprise") == null) {
				order.setEnterprise("个人");
			} else {
				order.setEnterprise("企业");
			}
			if (lists.get(i).get("give_reward") == null) {
				order.setGiveReward("￥0");
			} else {
				order.setGiveReward("￥" + lists.get(i).get("give_reward"));
			}
			if ("1".equals(lists.get(i).get("order_type"))) {
				map = orderDao.selectServerType(Integer.parseInt(lists.get(i).get("server_id").toString()));
			} else if ("2".equals(lists.get(i).get("order_type"))) {
				map = orderDao.selectDemandType(Integer.parseInt(lists.get(i).get("server_id").toString()));
			}
			if (map == null) {
				lists.remove(i);
				continue;
			}
			if (!StringTools.isEmpty(serverName) && !map.get("server_type").equals(serverName)) {
				lists.remove(i);
				continue;
			}
			order.setMobile(lists.get(i).get("mobile"));
			order.setRealityPrice(lists.get(i).get("reality_price"));
			order.setServerSubType(map.get("server_sub_type"));
			order.setServerTime(lists.get(i).get("server_time"));
			order.setServerType(map.get("server_type"));
			order.setStatus(lists.get(i).get("status"));
			order.setUsername(lists.get(i).get("username"));
			list2.add(order);
		}
		String[] titles = { "订单编号", "昵称", "手机号", "服务类型", "服务子类型", "预约人", "手机号", "认证类型", "预约时间", "提交时间", "金额", "打赏",
				"订单状态" };
		ExportExcel.exportExcel("订单列表.xls", titles, list2, response);
	}

	/**
	 * 获取子服务类型
	 */
	@RequestMapping(value="/subType", method=RequestMethod.POST)
	public ReturnData subType(String subtype) {
		List<Map<String,Object>> subType = orderService.subType(subtype);
		return new ReturnData(subType);
	}
}
